#!/bin/sh
#
# Run cscope for PCP source (src and qa)
#

tmp=/var/tmp/cscope.run.$$
trap "rm -f $tmp.*; exit 0" 0 1 2 3 15

rebuild=false
skip_libpcp3=false
if [ $# -eq 1 ]
then
    case "$1"
    in
	-b)	# rebuild files list and index
		rebuild=true
		shift
		;;
	-x)	# rebuild and exclude /usr/include
		rebuild=true
		export INCDIR=
		shift
		;;
	-3)	# rebuild and exclude src/libpcp3
		rebuild=true
		skip_libpcp3=true
		shift
		;;
    esac
fi

if [ $# -ne 0 ]
then
    echo >&2 "Usage: cscope.run [options]"
    echo >&2 "Options:"
    echo >&2 "-b	rebuild files list and index"
    echo >&2 "-x	exclude /usr/include, implies -b"
    echo >&2 "-3	exclude src/libpcp3, implies -b"
    exit 1
fi


if $rebuild || [ ! -f cscope.files ]
then
    rm -f cscope.out cscope.files
    git ls-files >$tmp.tmp
    find src qa -type f -a \( -name "*.[chly]" -o -name "*.h.in" -o -name "*.y.in" -o -name "*.cpp" \) \
    | while read file
    do
	if grep "^$file\$" $tmp.tmp >/dev/null
	then
	    echo "$file" >>cscope.files
	fi
    done
    if $skip_libpcp3
    then
	sed -i -e '/^src\/libpcp3\//d' cscope.files
    fi
    cscope -b -i cscope.files
fi

cscope -i cscope.files
